import { Module } from "vuex";
import { GlobalDataProps } from "./index";
import { set } from "lodash-es";
// 全局请求的状态
export interface GlobalStatus{
    requestNumber: number;
    opNames: {[key: string]: boolean};
    error: {
        status: boolean;
        message?: string;
    };
}

const global: Module<GlobalStatus,GlobalDataProps> = {
    state: {
        requestNumber: 0,
        opNames: {},
        error: {
           status: false, 
        }
    },
    mutations:{
        startLoading(state,{opName}){
            state.requestNumber++; 
            console.log('startLoading',opName, state.requestNumber);
            if(opName){
                state.opNames[opName] = true;
            }

        },
        finishLoading(state, {opName}){
            setTimeout(()=>{
                state.requestNumber--;
                if(opName){
                    delete state.opNames[opName];
                }
            },1000)
        },
        setError(state,e){
            state.error  = e
        }
    },
    getters:{
        isLoading(state){
            return state.requestNumber > 0;
        },
        isOpLoading(state){
            return (opName: string)=>{
                return state.opNames[opName];
            }
        }
    }

}

export default global;